using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UnitForm
{
    public partial class UnitForm : UTLBASEFORM.MDICHILD
    {
        #region "Private filed"
        private int _index = -1;
        private int _ID = -1;
        #endregion
        /// <summary>
        /// constructor init
        /// </summary>
        public UnitForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Load form method
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UnitForm_Load(object sender, EventArgs e)
        {
            SetupMenuBar();
            //UTLDB.UTLDB u = new UTLDB.UTLDB();
            //u.Connect();
            //grdData.DataSource = u.GetDataByStoredProcedure("sp_Select_tblUnit");
            //u.Disconnect();
            LoadData();
        }
        /// <summary>
        /// Setup menubar
        /// </summary>
        private void SetupMenuBar()
        {
            ShowBTN1(TextLabelAdd, ImageNameCheck_Ok);
            ShowBTN2(TextLabelEdit, ImageNameEdit);
            ShowBTN3(TextLabelDelete, ImageNameDelete);
        }
        /// <summary>
        /// event add unit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void btn1_Click(object sender, EventArgs e)
        {
            if (CheckRequire())
            {
                AddNewRow(this.txtName.Text, chbActive.Checked);
                ResetForm();
                ReloadData();
            }
            _ID = -1;
        }
        /// <summary>
        /// event add Edit unit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void btn2_Click(object sender, EventArgs e)
        {
            if (_ID != -1)
            {
                UpdateRow(_ID, this.txtName.Text, chbActive.Checked);
                ResetForm();
                ReloadData();
                _ID = -1;
            }
        }
        /// <summary>
        /// event delete unit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void btn3_Click(object sender, EventArgs e)
        {
            if (_ID != -1)
            {
                if (MsgBox("Ban muon xoa don vi '" + grdData.CurrentRow.Cells["colName"].Value.ToString() + "'", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    DeleteRow(_ID);
                    ResetForm();
                    ReloadData();
                    _ID = -1;
                }
            }
        }
        /// <summary>
        /// Load unit Data to form
        /// </summary>
        private void LoadData()
        {
            grdData.DataSource = GetData("sp_Select_tblUnit", null, null);
        }
        /// <summary>
        /// reload unit data to form
        /// </summary>
        private void ReloadData()
        {
            LoadData();
        }
        /// <summary>
        /// get data 
        /// </summary>
        /// <param name="strStoreName"></param>
        /// <param name="Para"></param>
        /// <returns></returns>
        private DataTable GetData(string strStoreName, string[] ParaName, string[] ParaValue)
        {
            UTLDB.UTLDB provider = new UTLDB.UTLDB();
            DataTable rs = new DataTable();
            try
            {
                if (ParaName != null && ParaValue != null)
                {
                    rs = provider.GetDataByStoredProcedure(strStoreName, ParaName, ParaValue);
                }
                else
                {
                    rs = provider.GetDataByStoredProcedure(strStoreName);
                }
            }
            catch (Exception ex)
            {
                //write log here
                MessageBox.Show(ex.ToString());
                rs = null;
            }
            return rs;
        }
        /// <summary>
        /// Event mouse click o grid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grdData_MouseClick(object sender, MouseEventArgs e)
        {
            _index = grdData.CurrentRow.Index;
            if (_index != -1)
            {
                EditRow(_index);
            }
        }
        /// <summary>
        /// Edit data on row
        /// </summary>
        /// <param name="_index"></param>
        private void EditRow(int _index)
        {
            try
            {
                _ID = int.Parse(grdData.Rows[_index].Cells["colID"].Value.ToString());
            }
            catch
            {
                _ID = -1;
            }
            txtName.Text = grdData.Rows[_index].Cells["colName"].Value.ToString();
            try
            {
                chbActive.Checked = bool.Parse(grdData.Rows[_index].Cells["colActive"].Value.ToString());
            }
            catch
            {
                chbActive.Checked = false;
            }
        }
        /// <summary>
        /// update data on row
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="active"></param>
        private void UpdateRow(int id, string name, bool active)
        {
            UTLDB.UTLDB provider = new UTLDB.UTLDB();
            string[] paraName = { "@ID", "@Name", "@Active" };
            object[] paravalue ={ id, name, active };
            provider.ExecuteNonQuery("sp_Update_tblUnit", paraName, paravalue);
        }
        /// <summary>
        /// Add new data
        /// </summary>
        /// <param name="name"></param>
        /// <param name="active"></param>
        private void AddNewRow(string name, bool active)
        {
            UTLDB.UTLDB provider = new UTLDB.UTLDB();
            string[] paraName = { "@Name", "@Active" };
            object[] paravalue ={ name, active };
            provider.ExecuteNonQuery("sp_Insert_tblUnit", paraName, paravalue);
        }
        /// <summary>
        /// delete row
        /// </summary>
        /// <param name="id"></param>
        private void DeleteRow(int id)
        {
            if (_ID != -1)
            {
                UTLDB.UTLDB provider = new UTLDB.UTLDB();
                string[] paraName = { "@ID" };
                object[] paravalue ={ id };
                provider.ExecuteNonQuery("sp_Delete_tblUnit", paraName, paravalue);
            }
        }
        /// <summary>
        /// check name of unit
        /// </summary>
        /// <returns></returns>
        private bool CheckRequire()
        {
            bool rs = true;
            if (txtName.Text.ToString() == "")
            {
                rs = false;
            }
            return rs;
        }
        /// <summary>
        /// reset form
        /// </summary>
        private void ResetForm()
        {
            this.txtName.Text = "";
            this.chbActive.Checked = false;
        }
    }
}